package org.example.struct.array;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author liyishan
 * @date 2023/12/27 09:16
 * @apiNote
 */

public class Test01356SortByBits {

    public int[] sortByBits(int[] arr) {
        int[] bit = new int[10001];
        List<Integer> list = new ArrayList<>();
        for(int x:arr){
            list.add(x);
            bit[x] = get(x);
        }
        Collections.sort(list,(a,b)-> bit[a] != bit[b] ? bit[a] -bit[b]:a-b);
        for(int i=0;i<arr.length;i++){
            arr[i] = list.get(i);
        }
        return arr;
    }

    public static int get(int x){
        int res = 0;
        while(x!=0){
            res +=x%2;
            x/=2;
        }
        return res;
    }
}
